package com.itawei.d4_charstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * 文件拷贝
 */
public class Demo2 {
    public static void main(String[] args) throws Exception {
        //1.创建对象
        File file1 = new File("C:\\Program Files\\Java\\src\\dsds");
        File file2 = new File("C:\\Program Files\\Java\\copysrc");

        copyFile(file1,file2);

    }
    public static void copyFile(File src, File dest)throws Exception{
        dest.mkdirs();

        //2.进入数据源
        File[] files = src.listFiles();
        //3.遍历数据源
        for (File file : files) {
            //4.判断 文件 拷贝
            if(file.isFile()){
                FileInputStream fis = new FileInputStream(src);//数据源
                FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));//目的地
                byte[] bytes = new byte[1024];
                int len;
                while((len = fis.read(bytes)) != -1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
                //文件夹 递归
            }else{
                copyFile(file,new File(file.getName()));
            }

        }
    }
}
